package com.hinotoyk.testsystem.controller;

import com.hinotoyk.testsystem.entity.Teacher;
import com.hinotoyk.testsystem.entity.TestPaperInfo;
import com.hinotoyk.testsystem.service.StudentService;
import com.hinotoyk.testsystem.service.TeacherService;
import com.hinotoyk.testsystem.uitls.TestPagerJson;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: testsystem
 * @description:
 * @author: DYJ
 * @create: 2019-11-21 20:02
 **/

@Controller
public class StudentController {

    @Resource
    private StudentService studentService;

    @Resource
    private TeacherService teacherService;

    @RequestMapping("/student/getAllTest")
    public String getAllTest(Model model, int studentId, int status) throws ParseException {
        List<TestPaperInfo> list = studentService.getAllTests(studentId, status);
        List<String> timeList = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        for(int i=0;i<list.size();i++){
            String startTime = TestPagerJson.MakeByJSONString(list.get(i).getTestPaperDetail()).getStartTime();
            Date date1 = sdf.parse(sdf.format(new Date()));
            Date date2 = sdf.parse(startTime);

            if(date1.getTime()==date2.getTime() || date1.getTime()>date2.getTime()){
                timeList.add("true");
            }else{
                timeList.add("false");
            }

        }

        List<Teacher> teacherList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            Integer teacherId = list.get(i).getTeacherId();
            Teacher teacher = teacherService.getTeacherById(teacherId);
            teacherList.add(teacher);
        }

        model.addAttribute("testList",list);
        model.addAttribute("timeList",timeList);
        model.addAttribute("teacherList",teacherList);
        model.addAttribute("studentId",studentId);
        if(status==0){
            return "/student/manageNotTest";
        }
        return "/student/manageHaveTest";
    }

}
    